#include "globalobject.h"
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QString>
#include <QStringList>
#include <QObject>
#include <QUuid>
using namespace Moolink;


static const QDate buildDate = QLocale( QLocale::English ).toDate( QString( __DATE__ ).replace( "  ", " 0" ), "MMM dd yyyy");
static const QTime buildTime = QTime::fromString( __TIME__, "hh:mm:ss" );

Station stationHW;
StationModel station;
SqliteHelper* sqldate;
NetWorkList netbox;
SerialList serialbox;
HostList hostlist;
CodeList codelist;
TimerThread stimer;
PortList portlist;
DeviceList devicelist;
CheckUpdate checkupdate;

GlobalObject::GlobalObject()
{

}

void GlobalObject::objectInit()
{

    //检查升级版本
    station.setSoftBuildOnTime(buildDate.toString("yyyy-MM-dd")+" "+buildTime.toString("hh:mm:ss"));
    station.setSoftCurrVer(buildDate.toString("yyMMdd")+buildTime.toString("hhmmss"));
    QObject::connect(&checkupdate,&CheckUpdate::needUpdate,&station,&StationModel::SoftUpdateDo);
    checkupdate.Check(station.getSoftCurrVer());

    //初始化数据库
    sqldate =new SqliteHelper();

    //初始化站点配置信息
    //stationHW = new Station();
    //station =&stationHW.stationInfo;//为兼容先前的代码，添加该指针，后期再修改
    QVariantList Slist=sqldate->Query("select * from station",0,1);
    if(Slist.count()>0) station.UpDateStationInfo(Slist.at(0).value<QVariantMap>());

    stimer.setCallbackFunc(Station::recvTimerCallBack);
    stimer.TimerRun(1000);//传值己无效,但仍保留传值功能,以便需要时使用

    //初始化上报平台列表
    QVariantList Nlist=GetData::getVariant_Server();
    qDebug()<<"Server Count In DataBase:"<<Nlist.count();
    if(Nlist.count()>0)
    {
        for(int i=0;i<Nlist.count();i++)
        {
            hostlist.addHost(Nlist.at(i));
        }
    }




    //初始化serial连接仓库-在本C文件顶部定义时已创建

    //初始化可用串口端口列表
    portlist.setText(serialbox.SerialRefresh());

    //初始化设备列表
    SetData::addNewDevice();

    //初始化因子列表
    SetData::addNewCode();
//    QVariantList Dlist=GetData::getVariant_Device();
//    qDebug()<<"Device Count In DataBase:"<<Dlist.count();
//    if(Dlist.count()>0)
//    {
//        for(int i=0;i<Dlist.count();i++)
//        {
//            devicelist.addDevice(Dlist.at(i));
//        }
//    }

    //初始化运行定时器
//    static QMetaObject::Connection connect(const QObject *sender, const char *signal,
//                        const QObject *receiver, const char *member, Qt::ConnectionType = Qt::AutoConnection);

//    static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal,
//                        const QObject *receiver, const QMetaMethod &method,
//                        Qt::ConnectionType type = Qt::AutoConnection);

//    inline QMetaObject::Connection connect(const QObject *sender, const char *signal,
//                        const char *member, Qt::ConnectionType type = Qt::AutoConnection) const;

//    stimer =new TimerThread();
//    //QObject::connect(stimer , SIGNAL(response2(const int Index)) , stationHW , SLOT(showRequest(const int Index))) ;
//    QObject::connect(stimer,&TimerThread::response2,&stationHW,&Station::showRequest);

}


